package com.example.demo.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.FileOutputStream;
import java.io.IOException;


/**
 * 继续
 *
 * @author adi
 * @since 2021/06/03 5:45
 */
public class exportPdf {
    public static void newPdf() {
        Document document = new Document();


        BaseFont bfChinese = null;
        try {
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        Phrase ph = new Phrase("中文显示", FontChinese);


//        File temFile = new File("F:\\adi\\a.pdf");
//        if(temFile.exists()){
//            temFile.delete();
//        }

        PdfWriter pdfWriter = null;
        try {
            PdfWriter.getInstance(document,new FileOutputStream("F:\\adi\\a.pdf"));
        }catch (Exception e){
            System.out.println("e = " + e);
        }


        document.open();

        Paragraph paragraph = new Paragraph("ssss");

        PdfPTable table = new PdfPTable(2);
        PdfPCell cell = new PdfPCell();
        cell.addElement(paragraph);
        table.addCell(cell);


        PdfPCell cell1 = new PdfPCell();
        cell1.addElement(new Paragraph("namw"));
        table.addCell(cell1);


        PdfPCell cell2 = new PdfPCell();
        cell2.addElement(ph);
        table.addCell(cell2);

        PdfPCell cell3 = new PdfPCell();
        cell3.addElement(new Paragraph(""));
        table.addCell(cell3);

        try{
            document.add(paragraph);
        }catch (DocumentException d){
            System.out.println("d = " + d);
        }

        try{
            document.add(table);
        }catch (DocumentException d){
            System.out.println("d = " + d);
        }


        document.close();

    }

    public static void main(String[] args) {
        newPdf();

        String a = "s" + DateFormatUtils.format(System.currentTimeMillis(),"yyyy:MM:dd ");


    }
}
